package study.javase.io;

import java.io.*;

/**
 *
 * 使用文件字节输入输出流拷贝文件
 * @author LiShaowen
 * @date 2020/9/8 22:31
 */
public class CopyFile2 {
    public static void main(String[] args) {
        // 1.创建源
       /* File origin = new File("src/main/java/study.javase/io/lsw.txt");
        File target = new File("src/main/java/study.javase/io/target.txt");*/
        File origin = new File("src/main/java/study.javase/io/LIshaowen.jpg");
        File target = new File("src/main/java/study.javase/io/lsw.jpg");
        // 2.选择流
        InputStream is = null;
        OutputStream os = null;

        try {
            is = new FileInputStream(origin);
            os = new FileOutputStream(target,true);
            // 操作（读取文件信息 然后 写入文件）
            int length;
            byte[] flush = new byte[5];
            StringBuilder stringBuilder = new StringBuilder();
            while (((length = is.read(flush)) != -1)) {
                os.write(flush,0,length);
            }
            byte[] datas = stringBuilder.toString().getBytes();
            os.write(datas,0,datas.length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null != is && null != os) {
                // 关闭资源，分别关闭，先打开的后关闭
                try {

                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    // 关闭资源，先打开的后关闭
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
